package com.example;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;


public class MailReaderService extends Service {

    private Timer timer = new Timer();

    private MailReaderServiceBinder mailReaderServiceBinder = new MailReaderServiceBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mailReaderServiceBinder;
    }

    public class MailReaderServiceBinder extends Binder implements IMailReaderService {
        @Override
        public void getMail() {
            System.out.println("++++++++++++!!!!!!!++++++++++++++++++++  SERVICE getMail +++++++++++++++++!!!!!!+++++++++++++++++");
        }
    }


    @Override
    public void onCreate() {
        super.onCreate();

        System.out.println("++++++++++++++++++++++++++++++++  SERVICE CREATED ++++++++++++++++++++++++++++++++++");


        startservice();
    }

    private void startservice() {
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("++++++++++++++++++++++++++++++++  SERVICE WORKS ++++++++++++++++++++++++++++++++++");
            }
        }, 0, 3000);

    }

    @Override
    public void onDestroy() {
        System.out.println("++++++++++++++++++++++++++++++++  SERVICE DESTROYED ++++++++++++++++++++++++++++++++++");
    }

    private void stopservice() {

        if (timer != null) {
            timer.cancel();
        }
    }


}
